<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="咨询人员" prop="consultant">
        <el-input
          v-model="queryParams.consultant"
          placeholder="请输入咨询人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属校区" prop="belongingToCampus">
        <el-input
          v-model="queryParams.belongingToCampus"
          placeholder="请输入归属校区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="毕业大学" prop="graduationFromUniversity">
        <el-input
          v-model="queryParams.graduationFromUniversity"
          placeholder="请输入毕业大学"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跟踪次数" prop="trackingTimes">
        <el-input
          v-model="queryParams.trackingTimes"
          placeholder="请输入跟踪次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['custom:clients:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['custom:clients:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['custom:clients:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['custom:clients:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="allocationConsultation"
          v-hasPermi="['custom:clients:allocationConsultation']"
        >分配咨询
        </el-button>
      </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            size="mini"
            @click="transfer"
            v-hasPermi="['custom:clients:transfer']"
          >移交
          </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="regularWorker"
          v-hasPermi="['custom:clients:regularWorker']"
        >转正
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"  :data="clientsList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="潜在客户ID" align="center" prop="id"/>
      <el-table-column label="姓名" align="center" prop="name">
          <template slot-scope="scope">
          <span @click="openDrawer(scope.row)">
          {{ scope.row.name }}
          </span>
          </template>
      </el-table-column>
      <el-table-column label="录入人" align="center" prop="inputterTeacher"/>
      <el-table-column label="咨询人员" align="center" prop="consultant"/>
      <el-table-column label="跟踪次数" align="center" prop="trackingTimes"/>
      <el-table-column label="最近跟踪记录" align="center" prop="trackRecord"/>
      <el-table-column label="QQ" align="center" prop="qq"/>
      <el-table-column label="手机号" align="center" prop="phoneNumber"/>
      <el-table-column label="微信" align="center" prop="wechat"/>
      <el-table-column label="未跟踪时间" align="center" prop="untracedTime"/>
      <el-table-column label="归属校区" align="center" prop="belongingToCampus"/>
      <el-table-column label="来源" align="center" prop="source"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['custom:clients:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['custom:clients:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="row-flex">
        <el-form-item label="姓名" prop="name" class="flex-item">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="性别" prop="gender" class="flex-item">
          <el-select v-model="form.gender" placeholder="请选择性别" style="width: 300px">
            <el-option label="男" value="Male"></el-option>
            <el-option label="女" value="Female"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age" class="flex-item">
          <el-input v-model="form.age" placeholder="请输入年龄"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber" class="flex-item">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="QQ" prop="qq" class="flex-item">
          <el-input v-model="form.qq" placeholder="请输入QQ"/>
        </el-form-item>
        <el-form-item label="微信" prop="wechat" class="flex-item">
          <el-input v-model="form.wechat" placeholder="请输入微信"/>
        </el-form-item>
        <el-form-item label="专业" prop="speciality" class="flex-item">
          <el-input v-model="form.speciality" placeholder="请输入专业"/>
        </el-form-item>
        <el-form-item label="学历" prop="education" class="flex-item">
          <el-select v-model="form.education" placeholder="请选择学历" style="width: 300px">
            <el-option label="本科" value="本科"></el-option>
            <el-option label="大专" value="大专"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="毕业时间" prop="graduationTime" class="flex-item">
          <el-date-picker clearable
                          v-model="form.graduationTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择毕业时间"
                          style="width: 300px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="毕业院校" prop="graduationFromUniversity" class="flex-item">
          <el-input v-model="form.graduationFromUniversity" placeholder="请输入毕业院校"/>
        </el-form-item>
        <el-form-item label="工作年限" prop="yearsOfService" class="flex-item">
          <el-input v-model="form.yearsOfService" placeholder="请输入工作年限"/>
        </el-form-item>
        <el-form-item label="来源" prop="source" class="flex-item">
          <el-input v-model="form.source" placeholder="请输入来源"/>
        </el-form-item>
        <el-form-item label="当前状态" prop="currentState" class="flex-item">
          <el-input v-model="form.currentState" placeholder="请输入当前状态"/>
        </el-form-item>
        <el-form-item label="是否老学员推荐" prop="reference" class="flex-item">
          <el-select v-model="form.reference" placeholder="请输入是否老学员推荐" style="width: 300px">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老学员" prop="seniorStudents" class="flex-item">
          <el-input v-model="form.seniorStudents" placeholder="请输入介绍人"/>
        </el-form-item>
        <el-form-item label="意向程度" prop="interestLevel" class="flex-item">
          <el-select v-model="form.interestLevel" placeholder="请输入意向程度" style="width: 300px;">
            <el-option label="高" value="High"></el-option>
            <el-option label="中" value="Medium"></el-option>
            <el-option label="低" value="Low"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="意向学科" prop="intendedSubject" class="flex-item">
          <el-select v-model="form.intendedSubject" placeholder="请输入意向学科" style="width: 300px">
            <el-option label="java" value="java"></el-option>
            <el-option label="python" value="python"></el-option>
            <el-option label="c" value="c"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="意向班级" prop="intendedClass" class="flex-item">
          <el-input v-model="form.intendedClass" placeholder="请输入意向班级"/>
        </el-form-item>
        <el-form-item label="下次跟进时间" prop="nextFollowUptime">
          <el-date-picker clearable
                          v-model="form.nextFollowUptime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择下次跟进时间"
                          style="width: 300px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks" style="width: 100%">
          <el-input type="textarea" v-model="form.remarks" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  <!--移交弹框-->
    <el-dialog
      title="移交信息"
      :visible.sync="dialogVisible"
      width="30%"
     >
      <el-form ref="form" :model="form" label-width="80px">

        <el-form-item label="移交老师" class="flex-item">
          <el-select v-model="form.consultant" placeholder="请选择移交老师">
            <el-option
              v-for="item in options"
              :key="item.name"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="modifyTeacher" >确 定</el-button>
      </span>
    </el-dialog>


  </div>

</template>

<script>
import {
  listClients,
  getClients,
  delClients,
  addClients,
  updateClients,
  addTeacherClients,
  saveTeacherClients, regularClients
} from "@/api/custom/clients";

export default {
  name: "Clients",
  data() {
    return {
      options:[],
      //移交参数
      dialogVisible: false,
      detailedInformation:{
        name: null,
        age: null,
        wechat: null,
        qq: null,
        phoneNumber: null,
        gender: null,
        intendedSubject: null,
        interestLevel: null,
        inputterTeacher: null,
        consultant: null,
        inputTime: null,
        lastFollowUpTime: null,
        intendedClass: null,
        source: null,
        education: null,
        graduationFromUniversity: null,
        graduationTime: null,
        reference: null,
        seniorStudents: null,
        speciality: null,
        yearsOfService: null,
        currentState: null,
        nextFollowUptime: null,
        trackingTimes: null,
        belongingToCampus: null,
        trackRecord: null,
        untracedTime: null
      },

      activeIndex: '1',
      drawer: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 潜在客户表格数据
      clientsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        age: null,
        wechat: null,
        qq: null,
        phoneNumber: null,
        gender: null,
        intendedSubject: null,
        interestLevel: null,
        inputterTeacher: null,
        consultant: null,
        inputTime: null,
        lastFollowUpTime: null,
        intendedClass: null,
        source: null,
        education: null,
        graduationFromUniversity: null,
        graduationTime: null,
        reference: null,
        seniorStudents: null,
        speciality: null,
        yearsOfService: null,
        currentState: null,
        nextFollowUptime: null,
        trackingTimes: null,
        belongingToCampus: null,
        trackRecord: null,
        untracedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        inputTime: [
          {required: true, message: "录入时间不能为空", trigger: "blur"}
        ],
        lastFollowUpTime: [
          {required: true, message: "最后跟踪时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    // 页面加载存储一次老师信息
    this.saveTeacher();
  },
  methods: {
    // 转正的方法
    regularWorker(){
      const ids = this.ids;
      regularClients(ids).then(response=>{

      })
    },
    saveTeacher(){
      saveTeacherClients().then(response=>{
      })
    },
    // 移交确认键
    modifyTeacher(){

      updateClients(this.form).then(response=>{
        this.dialogVisible=false;
        this.getList();
      })

    },

    // 移交
    transfer(){
      const ids = this.ids;
      this. dialogVisible=true;
      getClients(ids).then(response=>{
        console.log(response.data.consultant
        );
       this.form=response.data;
        saveTeacherClients().then(response=>{
          console.log(response)
          this.options=response;
        })
      })
    },
    // 分配咨询
    allocationConsultation(){
      const ids = this.ids;
      console.log(ids);
      addTeacherClients(ids).then(response=>{
        this.getList();
      })
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    openDrawer(row){
      console.log(row)
      this.drawer=true;
      getClients(row.id).then(response=>{
        console.log(response.data);
        this.detailedInformation=response.data;
      })
    },
    /** 查询潜在客户列表 */
    getList() {
      this.loading = true;
      listClients(this.queryParams).then(response => {
        this.clientsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        age: null,
        wechat: null,
        qq: null,
        phoneNumber: null,
        gender: null,
        intendedSubject: null,
        interestLevel: null,
        inputterTeacher: null,
        consultant: null,
        inputTime: null,
        lastFollowUpTime: null,
        intendedClass: null,
        source: null,
        education: null,
        graduationFromUniversity: null,
        graduationTime: null,
        reference: null,
        seniorStudents: null,
        speciality: null,
        yearsOfService: null,
        currentState: null,
        nextFollowUptime: null,
        trackingTimes: null,
        belongingToCampus: null,
        trackRecord: null,
        untracedTime: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加潜在客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClients(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改潜在客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClients(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClients(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除潜在客户编号为"' + ids + '"的数据项？').then(function () {
        return delClients(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('custom/clients/export', {
        ...this.queryParams
      }, `clients_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.row-flex {
  display: flex;
  flex-wrap: wrap;
}

.flex-item {
  flex: 1 0 calc(33.33% - 20px); /* 每个表单项占据三分之一宽度，减去间距 */
  margin-left: 20px;
  margin-bottom: 20px;
}

</style>
